<?php
  /**
   * @author 2mdc.com
   * @version 
   */

  include_once dirname(__FILE__) . '/cCreateElement.inc';

  class cCreateText extends cCreateElement {
  	const IDTITLE = 229998237;
    static private $rscInstance = NULL;
  	static private $intIdTitle = 0;
	 
	
    private function __construct() {
    }

    public function __destruct() {
    }

    public function __toString() {
      return $this->xml;
    }

    static public function getInstance() {
       if (self::$rscInstance == NULL) {
          self::$rscInstance = new cCreateText();
       }
       return self::$rscInstance;
    }

    protected function fGenerateB($strVal = 'single') {
      $this->xml = str_replace('__GENERATERPR__', '<'.cCreateElement::NAMESPACE.':b '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':b>__GENERATERPR__', $this->xml);
    }

    protected function fGenerateCOLOR($strVal = '000000') {
      $this->xml = str_replace('__GENERATERPR__', '<'.cCreateElement::NAMESPACE.':color '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':color>__GENERATERPR__', $this->xml);
    }

    protected function fGenerateJC($strVal = '') {
      $this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':jc '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':jc>__GENERATESZ__', $this->xml);
    }

    protected function fGenerateI($strVal = 'single') {
      $this->xml = str_replace('__GENERATERPR__', '<'.cCreateElement::NAMESPACE.':i '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':i>__GENERATERPR__', $this->xml);
    }

    protected function fGenerateSZ($strVal = '11') {
      $strVal *= 2;
      $this->xml = str_replace('__GENERATERPR__', '<'.cCreateElement::NAMESPACE.':sz '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':sz>__GENERATERPR__', $this->xml);
    }

    protected function fGenerateU($strVal = 'single') {
      $this->xml = str_replace('__GENERATERPR__', '<'.cCreateElement::NAMESPACE.':u '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':u>__GENERATERPR__', $this->xml);
    }
    
    protected function fGeneratePSTYLE($strVal = 'Ttulo') {
      $this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':pStyle '.cCreateElement::NAMESPACE.':val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':pStyle>__GENERATEPPR__', $this->xml);
    }
        
    protected function fGeneratePAGEBREAKBEFORE($strVal = 'on') {
      $this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':pageBreakBefore val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':pageBreakBefore>__GENERATEPPR__', $this->xml);
    }
        
    protected function fGenerateWIDOWCONTROL($strVal = 'on') {
      $this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':widowControl val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':widowControl>__GENERATEPPR__', $this->xml);
    }
      
    protected function fGenerateWORDWRAP($strVal = 'on') {
      $this->xml = str_replace('__GENERATEPPR__', '<'.cCreateElement::NAMESPACE.':wordWrap val="'.$strVal.'"></'.cCreateElement::NAMESPACE.':wordWrap>__GENERATEPPR__', $this->xml);
    }
      
    protected function fGenerateBOOKMARKSTART($intId, $name) {
      $this->xml = str_replace('__GENERATER__', '<'.cCreateElement::NAMESPACE.':bookmarkStart '.cCreateElement::NAMESPACE.':id="'.$intId.'" '.cCreateElement::NAMESPACE.':name="'.$name.'"></'.cCreateElement::NAMESPACE.':bookmarkStart>__GENERATER____GENERATEBOOKMARKEND__', $this->xml);
    }
      
    protected function fGenerateBOOKMARKEND($intId) {
      $this->xml = str_replace('__GENERATEBOOKMARKEND__', '<'.cCreateElement::NAMESPACE.':bookmarkEnd '.cCreateElement::NAMESPACE.':id="'.$intId.'"></'.cCreateElement::NAMESPACE.':bookmarkEnd>', $this->xml);
    }
        
    public function fCreateTitle(){
    	$this->xml = '';
    	$arrArgs = func_get_args();
    	if(!empty($arrArgs[1]['val']) && !empty($arrArgs[0])){
    		$this->fGenerateP();
    		$this->fGeneratePPR();
    		if($arrArgs[1]['type'] == 'subtitle')
    			$this->fGeneratePSTYLE('Subttulo' . $arrArgs[1]['val']);
    		else
    			$this->fGeneratePSTYLE('Ttulo' . $arrArgs[1]['val']);
    		if(!empty($arrArgs[1]['pageBreakBefore']))
	        	$this->fGeneratePAGEBREAKBEFORE($arrArgs[1]['pageBreakBefore']);
	    	if(!empty($arrArgs[1]['widowControl']))
	    		$this->fGenerateWIDOWCONTROL($arrArgs[1]['widowControl']);
	    	if(!empty($arrArgs[1]['wordWrap']))
	    		$this->fGenerateWORDWRAP($arrArgs[1]['wordWrap']);
	    	self::$intIdTitle++;
	    	$this->fGenerateBOOKMARKSTART(self::$intIdTitle, '_Toc'. (self::$intIdTitle + self::IDTITLE));
    		$this->fGenerateR();
	    	if($arrArgs[1]['b'] != '' || $arrArgs[1]['i'] != '' || $arrArgs[1]['u'] != '' || $arrArgs[1]['sz'] != '' || $arrArgs[1]['color'] != '') {
	          	$this->fGenerateRPR();
		        if($arrArgs[1]['b'] != '') {
		          $this->fGenerateB($arrArgs[1]['b']);
		        }
		        if($arrArgs[1]['i'] != '') {
		          $this->fGenerateI($arrArgs[1]['i']);
		        }
		        if($arrArgs[1]['u'] != '') {
		          $this->fGenerateU($arrArgs[1]['u']);
		        }
		        if($arrArgs[1]['sz'] != '') {
		          $this->fGenerateSZ($arrArgs[1]['sz']);
		        }
		        if($arrArgs[1]['color'] != '') {
		          $this->fGenerateCOLOR($arrArgs[1]['color']);
		        }
		    }
	        $this->fGenerateT($arrArgs[0]);
	        $this->fGenerateBOOKMARKEND(self::$intIdTitle);
	        $this->fCleanTemplate();
    	}else{
    		echo 'no has pasado el texto a introducir o el tipo de titulo a introducir';
    		return false;
    	}
    		
    }    
    
    public function fCreateText() {
      $this->xml = '';
      $arrArgs = func_get_args();

      $this->fGenerateP();
      if(!empty($arrArgs[1]['jc']) || !empty($arrArgs[1]['pageBreakBefore']) || !empty($arrArgs[1]['widowControl']) || !empty($arrArgs[1]['wordWrap'])) {
        $this->fGeneratePPR();
        if(!empty($arrArgs[1]['jc']))
        	$this->fGenerateJC($arrArgs[1]['jc']);
        if(!empty($arrArgs[1]['pageBreakBefore']))
        	$this->fGeneratePAGEBREAKBEFORE($arrArgs[1]['pageBreakBefore']);
    	if(!empty($arrArgs[1]['widowControl']))
    		$this->fGenerateWIDOWCONTROL($arrArgs[1]['widowControl']);
    	if(!empty($arrArgs[1]['wordWrap']))
    		$this->fGenerateWORDWRAP($arrArgs[1]['wordWrap']);
      }
      $this->fGenerateR();
      if($arrArgs[1]['b'] != '' || $arrArgs[1]['i'] != '' || $arrArgs[1]['u'] != '' || $arrArgs[1]['sz'] != '' || $arrArgs[1]['color'] != '') {
        $this->fGenerateRPR();
        if($arrArgs[1]['b'] != '') {
          $this->fGenerateB($arrArgs[1]['b']);
        }
        if($arrArgs[1]['i'] != '') {
          $this->fGenerateI($arrArgs[1]['i']);
        }
        if($arrArgs[1]['u'] != '') {
          $this->fGenerateU($arrArgs[1]['u']);
        }
        if($arrArgs[1]['sz'] != '') {
          $this->fGenerateSZ($arrArgs[1]['sz']);
        }
        if($arrArgs[1]['color'] != '') {
          $this->fGenerateCOLOR($arrArgs[1]['color']);
        }
      }
      $this->fGenerateT($arrArgs[0]);
    }
  }
?>